package be.webchess.www.actions.ajax;

import java.util.Map;

import be.webchess.www.logic.impl.ChessGameLogic;
import be.webchess.www.model.ChessBoard;
import be.webchess.www.model.ChessPiece;
import be.webchess.www.service.IChessBoardService;
import be.webchess.www.service.impl.ChessBoardService;

public class AjaxGameChessAction extends AjaxBaseAction {

	ChessBoard chess;
	ChessGameLogic	logic;
	IChessBoardService		cbs;	
	
	public AjaxGameChessAction() {
		// TODO Auto-generated constructor stub
	}

	public String execute ()
	{
		try 
		{
			if ( request.getParameter("chessboard_id") == null )
			{
				this.addActionError("Invalid action");
				return ERROR;			
			}			
			
			cbs = new ChessBoardService(connection);
			chess = cbs.findByID(Long.parseLong(request.getParameter("chessboard_id")));
			logic = new ChessGameLogic(chess);
				
		}catch ( Exception ex )
		{			
		}
		
		return SUCCESS;
	}
	
	public Map<String, ChessPiece> getPosition() {
		return logic.getChessBoardMatrix();
	}	
	
	public Long		getChessboardID()
	{
		return chess.getId();
	}
	
	public String	getCurrentPlayerColor()
	{
		return logic.getCurrentPlayerColor();
	}
	
	@Override
	public String getContent() {
		return "/WEB-INF/jsp/chessgame.jsp";
	}

}
